<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商代码" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入供应商代码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商类型" prop="supplierType">
        <el-select v-model="queryParams.supplierType" placeholder="请选择供应商类型" clearable style="width: 240px">
          <el-option
            v-for="(type, index) in supplierTypeEnum"
            :key="index"
            :label="type.label"
            :value="type.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="信用等级" prop="creditLevel">
        <el-select v-model="queryParams.creditLevel" placeholder="请选择信用等级" clearable style="width: 240px">
          <el-option
            v-for="(level, index) in creditLevelEnum"
            :key="index"
            :label="level.label"
            :value="level.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合作状态" prop="cooperationStatus">
        <el-select v-model="queryParams.cooperationStatus" placeholder="请选择合作状态" clearable style="width: 240px">
          <el-option
            v-for="(status, index) in cooperationStatusEnum"
            :key="index"
            :label="status.label"
            :value="status.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商状态" prop="supplierStatus">
        <el-select v-model="queryParams.supplierStatus" placeholder="请选择供应商状态" clearable style="width: 240px">
          <el-option
            v-for="(status, index) in supplierStatusEnum"
            :key="index"
            :label="status.label"
            :value="status.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wms:supplier:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商ID" align="center" prop="supplierId" v-if="false" />
      <el-table-column label="供应商代码" align="center" prop="supplierCode" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="供应商类型" align="center" prop="supplierType">
        <template #default="scope">
          <dict-tag :options="supplierTypeEnum" :value="scope.row.supplierType" />
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="信用等级" align="center" prop="creditLevel">
        <template #default="scope">
          <dict-tag :options="creditLevelEnum" :value="scope.row.creditLevel" />
        </template>
      </el-table-column>
      <el-table-column label="合作状态" align="center" prop="cooperationStatus">
        <template #default="scope">
          <dict-tag :options="cooperationStatusEnum" :value="scope.row.cooperationStatus" />
        </template>
      </el-table-column>
      <el-table-column label="供应商状态" align="center" prop="supplierStatus">
        <template #default="scope">
          <dict-tag :options="supplierStatusEnum" :value="scope.row.supplierStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:supplier:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="supplierRef" :model="form" :rules="rules" label-width="100px">
        <el-tabs v-model="activeName">
          <el-tab-pane label="基本信息" name="basic">
            <el-row>
              <el-col :span="12">
                <el-form-item label="供应商代码" prop="supplierCode">
                  <el-input v-model="form.supplierCode" placeholder="请输入供应商代码" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供应商名称" prop="supplierName">
                  <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="供应商类型" prop="supplierType">
                  <el-select v-model="form.supplierType" placeholder="请选择供应商类型" style="width: 100%">
                    <el-option
                      v-for="(type, index) in supplierTypeEnum"
                      :key="index"
                      :label="type.label"
                      :value="type.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="营业执照号" prop="businessLicense">
                  <el-input v-model="form.businessLicense" placeholder="请输入营业执照号" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="税号" prop="taxNumber">
                  <el-input v-model="form.taxNumber" placeholder="请输入税号" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人代表" prop="legalPerson">
                  <el-input v-model="form.legalPerson" placeholder="请输入法人代表" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="联系信息" name="contact">
            <el-row>
              <el-col :span="12">
                <el-form-item label="联系人" prop="contactPerson">
                  <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="contactPhone">
                  <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="联系邮箱" prop="contactEmail">
                  <el-input v-model="form.contactEmail" placeholder="请输入联系邮箱" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="省份" prop="province">
                  <el-input v-model="form.province" placeholder="请输入省份" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="城市" prop="city">
                  <el-input v-model="form.city" placeholder="请输入城市" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="区县" prop="district">
                  <el-input v-model="form.district" placeholder="请输入区县" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="详细地址" prop="address">
                  <el-input v-model="form.address" type="textarea" placeholder="请输入详细地址" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="银行信息" name="bank">
            <el-row>
              <el-col :span="12">
                <el-form-item label="开户银行" prop="bankName">
                  <el-input v-model="form.bankName" placeholder="请输入开户银行" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="银行账号" prop="bankAccount">
                  <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="状态信息" name="status">
            <el-row>
              <el-col :span="12">
                <el-form-item label="信用等级" prop="creditLevel">
                  <el-select v-model="form.creditLevel" placeholder="请选择信用等级" style="width: 100%">
                    <el-option
                      v-for="(level, index) in creditLevelEnum"
                      :key="index"
                      :label="level.label"
                      :value="level.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="合作状态" prop="cooperationStatus">
                  <el-select v-model="form.cooperationStatus" placeholder="请选择合作状态" style="width: 100%">
                    <el-option
                      v-for="(status, index) in cooperationStatusEnum"
                      :key="index"
                      :label="status.label"
                      :value="status.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="供应商状态" prop="supplierStatus">
                  <el-select v-model="form.supplierStatus" placeholder="请选择供应商状态" style="width: 100%">
                    <el-option
                      v-for="(status, index) in supplierStatusEnum"
                      :key="index"
                      :label="status.label"
                      :value="status.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="备注信息" name="remark">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Supplier">
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier, exportSupplier } from "@/api/wms/base/supplier";
import { parseTime } from '@/utils/ruoyi';

const { proxy } = getCurrentInstance();

// 供应商类型枚举
const supplierTypeEnum = [
  { value: "1", label: "生产商" },
  { value: "2", label: "经销商" },
  { value: "3", label: "代理商" }
];

// 信用等级枚举
const creditLevelEnum = [
  { value: "A", label: "A" },
  { value: "B", label: "B" },
  { value: "C", label: "C" },
  { value: "D", label: "D" }
];

// 合作状态枚举
const cooperationStatusEnum = [
  { value: "1", label: "潜在" },
  { value: "2", label: "试用" },
  { value: "3", label: "正式" },
  { value: "4", label: "暂停" },
  { value: "5", label: "终止" }
];

// 供应商状态枚举
const supplierStatusEnum = [
  { value: "1", label: "待审核" },
  { value: "2", label: "已审核" },
  { value: "3", label: "已禁用" }
];

const supplierList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const activeName = ref("basic");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supplierCode: null,
    supplierName: null,
    supplierType: null,
    creditLevel: null,
    cooperationStatus: null,
    supplierStatus: null
  },
  rules: {
    supplierCode: [
      { required: true, message: "供应商代码不能为空", trigger: "blur" }
    ],
    supplierName: [
      { required: true, message: "供应商名称不能为空", trigger: "blur" }
    ],
    supplierType: [
      { required: true, message: "供应商类型不能为空", trigger: "change" }
    ],
    businessLicense: [
      { required: true, message: "营业执照号不能为空", trigger: "blur" }
    ],
    taxNumber: [
      { required: true, message: "税号不能为空", trigger: "blur" }
    ],
    legalPerson: [
      { required: true, message: "法人代表不能为空", trigger: "blur" }
    ],
    contactPerson: [
      { required: true, message: "联系人不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" },
      { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
    ],
    contactEmail: [
      { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" }
    ],
    province: [
      { required: true, message: "省份不能为空", trigger: "blur" }
    ],
    city: [
      { required: true, message: "城市不能为空", trigger: "blur" }
    ],
    district: [
      { required: true, message: "区县不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "详细地址不能为空", trigger: "blur" }
    ],
    creditLevel: [
      { required: true, message: "信用等级不能为空", trigger: "change" }
    ],
    cooperationStatus: [
      { required: true, message: "合作状态不能为空", trigger: "change" }
    ],
    supplierStatus: [
      { required: true, message: "供应商状态不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询供应商列表 */
function getList() {
  loading.value = true;
  listSupplier(queryParams.value).then(response => {
    supplierList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    supplierId: null,
    supplierCode: null,
    supplierName: null,
    supplierType: null,
    businessLicense: null,
    taxNumber: null,
    legalPerson: null,
    contactPerson: null,
    contactPhone: null,
    contactEmail: null,
    province: null,
    city: null,
    district: null,
    address: null,
    bankName: null,
    bankAccount: null,
    creditLevel: null,
    cooperationStatus: null,
    supplierStatus: null,
    remark: null
  };
  proxy.resetForm("supplierRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.supplierId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加供应商";
  activeName.value = "basic";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _supplierId = row.supplierId || ids.value[0];
  getSupplier(_supplierId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改供应商";
    activeName.value = "basic";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["supplierRef"].validate(valid => {
    if (valid) {
      if (form.value.supplierId != null) {
        updateSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _supplierIds = row.supplierId || ids.value;
  proxy.$modal.confirm('是否确认删除供应商编号为"' + _supplierIds + '"的数据项？').then(function() {
    return delSupplier(_supplierIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/base/supplier/export', {
    ...queryParams.value
  }, `supplier_${new Date().getTime()}.xlsx`);
}

getList();
</script>